/*
 * Copyright (c) 2010, Michael Hengler (developer (at) hengler (dot) de),
 * Nikolaus Moll (developer (at) miblounge (dot) net).
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * * Neither the name of the Jombi project nor the
 * names of its contributors may be used to endorse or promote products
 * derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL JOMBI BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */
package net.miblounge.jombi.impl.widgets;

import net.miblounge.jombi.impl.layout.LayoutManagerImpl;
import net.miblounge.jombi.impl.layout.VerySimpleLayout;
import net.miblounge.jombi.impl.util.BorderToComposite;
import net.miblounge.jombi.impl.util.ColorUtil;
import net.miblounge.miglayout.qt.MigLayout;

import org.jowidgets.common.widgets.IWidgetCommon;
import org.jowidgets.common.widgets.descriptor.setup.ICompositeSetupCommon;
import org.jowidgets.common.widgets.factory.IGenericWidgetFactory;
import org.jowidgets.common.widgets.layout.ILayoutDescriptor;
import org.jowidgets.common.widgets.layout.ILayouter;
import org.jowidgets.common.widgets.layout.MigLayoutDescriptor;
import org.jowidgets.util.Assert;

import com.trolltech.qt.gui.QGroupBox;
import com.trolltech.qt.gui.QLayout;
import com.trolltech.qt.gui.QPalette;
import com.trolltech.qt.gui.QPalette.ColorRole;
import com.trolltech.qt.gui.QWidget;
import com.trolltech.qt.gui.QWidgetItem;

public class CompositeImpl extends QtComposite {

	private QLayout innerLayout = null;
	private final QWidget innerWidget;

	public CompositeImpl(final Object parentUiReference, final IGenericWidgetFactory factory, final ICompositeSetupCommon setup) {
		super(factory, BorderToComposite.convert(setup.getBorder()));
		getUiReference().setParent((QWidget) parentUiReference);
		// Border code in BorderToComposite
		// getUiReference().setBorder(BorderConvert.convert(setup.getBorder()));

		// Performance-Tweak for Qt
		if (getUiReference() instanceof QGroupBox) {
			final VerySimpleLayout outerLayout = new VerySimpleLayout(getUiReference());
			getUiReference().setLayout(outerLayout);

			innerWidget = new QWidget(getUiReference()) {
				@Override
				public String toString() {
					return "QWidget [inner Widget]";
				}
			};
			outerLayout.addItem(new QWidgetItem(innerWidget));

			// reset the palette of the inner widget
			final QPalette innerPalette = new QPalette(getUiReference().palette());
			innerPalette.setColor(ColorRole.WindowText, ColorUtil.getDefaultColor(ColorRole.WindowText));
			innerWidget.setPalette(innerPalette);
		} else {
			innerWidget = getUiReference();
		}
	}

	// Qt fix
	@Override
	public void setLayout(final ILayoutDescriptor layoutManager) {
		Assert.paramNotNull(layoutManager, "layoutManager");

		// delete old layout if necessary
		if (innerLayout != null) {
			innerLayout.dispose();
		}
		if (innerWidget.layout() != null) {
			innerWidget.layout().dispose();
		}

		if (layoutManager instanceof MigLayoutDescriptor) {
			final MigLayoutDescriptor migLayoutManager = (MigLayoutDescriptor) layoutManager;
			innerLayout = new MigLayout(
				migLayoutManager.getLayoutConstraints(),
				migLayoutManager.getColumnConstraints(),
				migLayoutManager.getRowConstraints());
			innerWidget.setLayout(innerLayout);
		} else if (layoutManager instanceof ILayouter) {
			innerLayout = new LayoutManagerImpl(getUiReference(), (ILayouter) layoutManager);
			innerWidget.setLayout(innerLayout);
		} else {
			throw new IllegalArgumentException("Layout Manager of type '"
				+ layoutManager.getClass().getName()
				+ "' is not supported");
		}
	}

	// Qt fix
	@Override
	protected void addToContainer(final Integer index, final IWidgetCommon widget, final Object cellConstraints) {
		// TODO NM index
		if (innerLayout instanceof MigLayout) {
			final MigLayout layout = (MigLayout) innerLayout;

			if (cellConstraints != null) {
				layout.addWidget((QWidget) widget.getUiReference(), cellConstraints);
			} else {
				layout.addWidget((QWidget) widget.getUiReference());
			}
		} else if (innerLayout instanceof LayoutManagerImpl) {
			final LayoutManagerImpl layout = (LayoutManagerImpl) innerLayout;
			layout.addLayoutComponent((QWidget) widget.getUiReference(), cellConstraints);
		} else {
			throw new IllegalArgumentException("Layout Manager of type '"
				+ innerLayout.getClass().getName()
				+ "' is not supported");
		}
	}
}
